package cn.yfxu.server.service.impl;
import java.time.LocalDateTime;

import cn.yfxu.server.mapper.MailLogMapper;
import cn.yfxu.server.pojo.*;
import cn.yfxu.server.mapper.EmployeeMapper;
import cn.yfxu.server.service.IEmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yfxu
 * @since 2021-03-10
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
     @Autowired
     private RabbitTemplate rabbitTemplate;
    @Autowired
    private MailLogMapper mailLogMapper;
    @Override
    public PageResult getEmployeeByPage(Integer currentPage, Integer size, Employee employee, LocalDate[] beginDateScope) {
        Page<Employee> page = new Page<>(currentPage, size);
        IPage<Employee> employeeByPage = employeeMapper.getEmployeeByPage(page, employee, beginDateScope);
        PageResult result = new PageResult(employeeByPage.getTotal(), employeeByPage.getRecords());
        return result;
    }

    @Override
    public Result maxWorkID() {
        List<Map<String, Object>> maps = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workID)"));
        return Result.success(null, String.format("%08d", Integer.parseInt(maps.get(0).get("max(workID)").toString()+ 1)));
    }

    @Override
    public Result addEmp(Employee employee) {
        //处理合同期限，保留两位小数
        LocalDate beginContract = employee.getBegincontract();
        LocalDate endContract = employee.getEndcontract();
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractterm(Double.parseDouble(decimalFormat.format(days / 365.00)));
        if (1 == employeeMapper.insert(employee)) {

//            Employee emp=employeeMapper.getEmployee(employee.getId()).get(0);
//            String msgId= UUID.randomUUID().toString();
//            MailLog maillog=new MailLog();
//            maillog.setMsgid(msgId);
//            maillog.setEid(employee.getId());
//            maillog.setStatus(0);
//            maillog.setRoutekey(MailConstants.MAIL_ROUTING_KEY_NAME);
//            maillog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
//            maillog.setCount(0);
//            maillog.setTrytime(LocalDateTime.now().plusMinutes(MailConstants.MSG_TIMEOUT));
//            maillog.setCreatetime(LocalDateTime.now());
//            maillog.setUpdatetime(LocalDateTime.now());
//            mailLogMapper.insert(maillog);
//            //发送信息
//            rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME,MailConstants.MAIL_ROUTING_KEY_NAME,emp,new CorrelationData(msgId));
            return Result.success("添加成功！");
        }
        return Result.success("添加失败！");
    }

    @Override
    public List<Employee> getEmployee(Integer id) {

        return employeeMapper.getEmployee(id);
    }
}
